package com.chixing.day12_date_factory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 封装了项目中日期相关的操作
 * 如日期与字符串的转换，获得日期中的年月日时分秒
 */
public class DateUtil {


    /**
     * 字符串转化成 LocalDateTime对象
     */
    public static LocalDateTime stringToLocalDateTime(String str ){ // 2021/10/4
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") ;
        return LocalDateTime.parse(str,formatter);
    }
    /**
     * 字符串转化成 LocalDate对象
     */
    public static LocalDate stringToLocalDate(String str ) { // 2021/10/4
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        return LocalDate.parse(str, formatter);
    }
    /**
     * 字符串转化成 LocalDate对象
     */
    public static LocalTime stringToLocalTime(String str ) { // 2021/10/4
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        return LocalTime.parse(str, formatter);
    }


    /**
     * 将 LocalDateTime 转换成字符串格式
     */
    public static String localDateTimeToString(LocalDateTime localDateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") ;
        return   localDateTime.format(formatter);
    }
    /**
     * 将 LocalDate 转换成字符串格式
     */
    public static String localDateToString(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd") ;
        return   localDate.format(formatter);
    }
    /**
     * 将 LocalTime 转换成字符串格式
     */
    public static String localTimeToString(LocalTime localTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss") ;
        return   localTime.format(formatter);
    }



}
